package cn.git.future;

import java.util.concurrent.*;

/**
 * @description: 串行任务编排，任务有先后顺序
 * @program: bank-credit-sy
 * @author: lixuchun
 * @create: 2024-08-07
 */
public class CompletableFutureDemo04 {

    static ThreadPoolExecutor executor = new ThreadPoolExecutor(
            5,
            50,
            10,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(100),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 没有返回结果，可以执行任务 线程1结束执行线程2
        CompletableFuture.runAsync(() -> {
            System.out.println("子线程1执行了：" + Thread.currentThread().getName());
        }, executor).thenRunAsync(()-> {
            System.out.println("子线程2执行了：" + Thread.currentThread().getName());
        }, executor);

        // 有返回结果，可以执行任务 线程1结束执行线程4
        CompletableFuture.supplyAsync(() -> {
            System.out.println("子线程3执行了：" + Thread.currentThread().getName());
            return 100;
        }, executor).thenAcceptAsync((param) -> {
            // 线程3执行结果作为线程4入参参数
            System.out.println("子线程4执行了：" + Thread.currentThread().getName());
            System.out.println("线程4参数 = " + param);
        }, executor);


        // 有返回结果，获取处理
        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("子线程5执行了：" + Thread.currentThread().getName());
            return 100;
        }, executor).thenApplyAsync((param) -> {
            // 线程3执行结果作为线程4入参参数
            System.out.println("子线程6执行了：" + Thread.currentThread().getName());
            System.out.println("线程6参数 = " + param);
            return param * 100;
        }, executor);

        System.out.println("thread6 result = " + integerCompletableFuture.get());
        executor.shutdown();
    }
}
